﻿<?php

include("utils.php");

    if($_SESSION['home']=="games")
    {
      if(!isset($_POST['HOME_ID'])||$_POST['HOME_ID']=="")
      {
        print "<BR>LIST OF GAMES FOR THIS USER: <BR>\n";
        $sql_games = "select tt_games.* 
                      from tt_games, tt_games_herds, tt_users 
                      where tt_games_herds.game = tt_games.id and
                            tt_games.sts < 9 and
                            tt_games_herds.user = tt_users.id and
                            tt_users.id = '".$_SESSION['userid']."'";
        $tt_games = mysql_query($sql_games);
        print mysql_error(); 
        if(mysql_num_rows($tt_games)>0)
        {
          # risujem spisok vseh igr u igroka
          print "<TABLE BORDER='1'>
                   <TR>
                     <TD>ID</TD>
                     <TD>TYPE</TD>
                     <TD>NAME</TD>
                     <TD>STATUS</TD>
                     <TD>YEAR</TD>
                     <TD>USERS</TD>
                     <TD>GAMERS</TD>
                   <TR>";
          while($tt_game = mysql_fetch_array($tt_games))
          {
            $gusers = "";
            $sql_users = "select tt_games_herds.nik 
                          from tt_games_herds 
                          where tt_games_herds.game = ".$tt_game['id'];
            $tt_users = mysql_query($sql_users);
            while($tt_user = mysql_fetch_array($tt_users))
            {
              if($gusers!="")
              { 
                $gusers = $gusers.",";
              }
              $gusers = $gusers.$tt_user['nik'];
            }
            if($gusers=="")
            {
              $gusers = "no users";
            }
            if(/* $tt_game['sts'] */ 1==1)
            {
              print "  <TR onmouseover='this.style.cursor=\"hand\"' 
                           onclick='document.HOME_FORM.HOME_FIELD.value=\"games\";
                                    document.HOME_FORM.HOME_ID.value=\"".$tt_game['id']."\";
                                    document.HOME_FORM.submit()'>";
            }
            else
            {
              print "  <TR>";
            }
            print "  <TD>".$tt_game['id']."</TD>
                       <TD>".$tt_game['type']."</TD>
                       <TD>".$tt_game['name']."</TD>
                       <TD>".$tt_game['sts']."</TD>
                       <TD>".$tt_game['year']."</TD>
                       <TD>".$tt_game['ucount']."</TD>
                       <TD>".$gusers."</TD>
                     <TR>";
          }
          print "</TABLE>";
        }
        else
        {
          # pishem, chto netu igr
          print " no games for this user :( <BR>\n";
        }
      }
      else
      {
        $gameid = mysql_real_escape_string($_POST['HOME_ID']);
        $sql_games = "select * from tt_games where tt_games.id = ".$gameid;
        $tt_games = mysql_query($sql_games);
        print mysql_error(); 
        if(mysql_num_rows($tt_games)>0)
        {
          $tt_game = mysql_fetch_array($tt_games);
          if($tt_game['sts']==0)
          {
            $game_result = 0;
            $sql_users = "select * from tt_games_herds 
                          where tt_games_herds.sts = 2 and 
                                tt_games_herds.game = ".$gameid;
            $tt_users = mysql_query($sql_users);
            print mysql_error();
            while($tt_user = mysql_fetch_array($tt_users))
            {
              $game_result = $game_result  + 1;
              $winner[$game_result] = $tt_user['nik'];
            }
            if($game_result == 0)
            {
              print "<BR><p class='header'>Игра окончена, победил сервер.</p><BR>";
            }
            else
            {
              print "<BR><p class='header'>Игра окончена.<BR>Победители:</p>";
            }
            for($i=1;$i<=count($winner);$i++)
            {
              print "<p class='header'>".$winner[$i]."</p>";
            }
          }
          print "<BR>";
          print "<TABLE BORDER='1'>
                   <TR>
                     <TD>TYPE: ".$tt_game['type']."</TD>
                     <TD>NAME: ".$tt_game['name']."</TD>
                     <TD>YEAR: ".$tt_game['year']."</TD>
                     <TD>USERS: ".$tt_game['ucount']."</TD>
                     <TD>CARDS: ".$tt_game['ccount']."</TD>
                   <TR>
                 </TABLE>";
          if(/* $tt_game['sts']*/ 1==1)
          {
            # pechatajem informaciju pro tekushchij god
            print "<BR>";
            $sql_users = "select * from tt_games_herds 
                          where tt_games_herds.user = ".$_SESSION['userid']." and 
                                tt_games_herds.game = ".$gameid;
            $tt_users = mysql_query($sql_users);
            print mysql_error(); 
            if(mysql_num_rows($tt_users)>0)
            {
              $tt_user = mysql_fetch_array($tt_users);
              $my_id = $tt_user["id"];
              $my_nik = $tt_user["nik"];
              $tt_live = $tt_user["sts"];
              $sql_gcards = "select tt_cards.name,tt_actions.action, tt_cards_actions.param
                       from tt_games_cards,tt_cards,tt_cards_actions,tt_actions 
                       where tt_games_cards.sts = 1 and
                             (
                              (
                               tt_games_cards.on_herd  = ".$tt_user["id"]."
                              )
                              or
                              (
                               tt_games_cards.on_herd  <> ".$tt_user["id"]." and
                               tt_cards.class = 3
                              )
                             ) and
                             tt_games_cards.game = ".$gameid." and
                             tt_cards.id = tt_games_cards.card and
                             tt_cards.time = 13 and
                             tt_cards_actions.card = tt_cards.id and
                             tt_actions.id = tt_cards_actions.action
                             ";
              $tt_gcards = mysql_query($sql_gcards);
              print mysql_error();
              while($tt_gcard = mysql_fetch_array($tt_gcards))
              {
                # print $tt_gcard["name"].$tt_gcard["action"].$tt_gcard["param"]."<br>";
                $id = $tt_gcard["action"];
                if($id=='PZ')
                {
                 $tt_user['PZ'] = $tt_user['PZ'] + $tt_gcard["param"];           
                }
                if($id=='PV')
                {
                 $tt_user['PV'] = $tt_user['PV'] + $tt_gcard["param"];           
                }
                if($id=='B')
                {
                 $tt_user['B'] = $tt_user['B'] + $tt_gcard["param"];           
                }
                if($id=='E')
                {
                 $tt_user['E'] = $tt_user['E'] + $tt_gcard["param"];           
                }
                if($id=='PT')
                {
                 $tt_user['PT'] = $tt_user['PT'] + $tt_gcard["param"];           
                }
                if($id=='TZ')
                {
                 $tt_user['TZ'] = $tt_user['TZ'] + $tt_gcard["param"];           
                }
              }

              #$td = "<TR><TD>%s</TD><TD>%s</TD><TD>%s</TD></TR>";
              $td = "<TR bgcolor='#003366'><TD width='200'>%s</TD><TD width='200' align='right'>%s</TD></TR>";
              print "<p class='header'>Ваше стадо</p>";
              print "<table border='0' cellspacing='0' cellpadding='0' bgcolor='#BEDEFF'><tr><td>";
              print "<TABLE border='0' width='100%' cellpadding='2' cellspacing='1'>";
              #printf($td,"PARAMETER","MODIFICATOR","VALUE");
              printf($td,"Игрок",$tt_user['nik']);
              printf($td,"Текущий год",get_type_name($tt_game['year']));
              printf($td,"Следующий год",get_type_name($tt_game['year'] + 1));              
              if($tt_user['sts']==9)
              {
                printf($td,"Статус игрока","Умер");
              }
              else
              {
                $balance_v = round($tt_user['PZ'] * $tt_user['B'] / 100,0);
                $balance_z = floor($tt_user['PV'] * $tt_user['B'] * $tt_user['E'] / 100 * (-1));
                printf($td,"Зайцев (Z)",$tt_user['Z']." шт (".$balance_z.")");
                printf($td,"Волков (V)",$tt_user['V']." шт (".$balance_v.")");
                printf($td,"ТрынТравы (TT)",($tt_user['TT'])." кг");

                printf($td,"Прирост зайцев (PZ)",$tt_user['PZ']."%");
                printf($td,"Смертность волков (PV)",$tt_user['PV']."%");

                printf($td,"Скрытность (B)",$tt_user['B']);
                printf($td,"Порция зайцев (E)",$tt_user['E']." шт");
                printf($td,"Порча ТрынТравы (PT)",$tt_user['PT']."%");
                printf($td,"Сбор (TZ)",$tt_user['TZ']." г/заяц");                
              }
              print "</TABLE>";
              print "</td></tr></table>";
            }
            # pechatajem informaciju pro tekushchij god
            print "<BR>";
            $sql_users = "select * from tt_games_herds where tt_games_herds.game = ".$gameid;
            $tt_users = mysql_query($sql_users);
            print mysql_error(); 
            if(mysql_num_rows($tt_users)>0)
            {
              $td = "<TR bgcolor='#003366'><TD>%s</TD><TD>%s</TD><TD>%s</TD><TD>%s</TD><TD>%s</TD><TD>%s</TD><TD>%s</TD><TD>%s</TD><TD>%s</TD><TD>%s</TD><TD>%s</TD></TR>";
              print "<p class='header'>Все стада</p>";

              print "<table border='0' cellspacing='0' cellpadding='0' bgcolor='#BEDEFF'><tr><td>";
              print "<TABLE border='0' width='100%' cellpadding='2' cellspacing='1'>";
                  printf($td,
                         "Id",
                         "Стадо",
                         "Z",
                         "V",
                         "TT",
                         "PZ",
                         "PV",
                         "B",
                         "E",
                         "PT",
                         "TZ");
              while($tt_user = mysql_fetch_array($tt_users))
              {
                if($tt_user['sts']==9)
                {
                  printf($td,
                         $tt_user['npp'],
                         $tt_user['nik']." (Умер)");
                }
                else
                {
                  $sql_gcards = "select tt_cards.name,tt_actions.action, tt_cards_actions.param
                       from tt_games_cards,tt_cards,tt_cards_actions,tt_actions 
                       where tt_games_cards.sts = 1 and
                             (
                              (
                               tt_games_cards.on_herd  = ".$tt_user["id"]."
                              )
                              or
                              (
                               tt_games_cards.on_herd  <> ".$tt_user["id"]." and
                               tt_cards.class = 3
                              )
                             ) and
                             tt_games_cards.game = ".$gameid." and
                             tt_cards.id = tt_games_cards.card and
                             tt_cards.time = 13 and
                             tt_cards_actions.card = tt_cards.id and
                             tt_actions.id = tt_cards_actions.action
                             ";
                  $tt_gcards = mysql_query($sql_gcards);
                  print mysql_error();
                  while($tt_gcard = mysql_fetch_array($tt_gcards))
                  {
                    # print $tt_gcard["name"].$tt_gcard["action"].$tt_gcard["param"]."<br>";
                    $id = $tt_gcard["action"];
                    if($id=='PZ')
                    {
                     $tt_user['PZ'] = $tt_user['PZ'] + $tt_gcard["param"];           
                    }
                    if($id=='PV')
                    {
                     $tt_user['PV'] = $tt_user['PV'] + $tt_gcard["param"];           
                    }
                    if($id=='B')
                    {
                     $tt_user['B'] = $tt_user['B'] + $tt_gcard["param"];           
                    }
                    if($id=='E')
                    {
                     $tt_user['E'] = $tt_user['E'] + $tt_gcard["param"];           
                    }
                    if($id=='PT')
                    {
                     $tt_user['PT'] = $tt_user['PT'] + $tt_gcard["param"];           
                    }
                    if($id=='TZ')
                    {
                     $tt_user['TZ'] = $tt_user['TZ'] + $tt_gcard["param"];           
                    }
                  }
                  printf($td,
                         $tt_user['npp'],
                         $tt_user['nik'],
                         $tt_user['Z'],
                         $tt_user['V'],
                         $tt_user['TT'],
                         $tt_user['PZ']."%",
                         $tt_user['PV']."%",
                         $tt_user['B'],
                         $tt_user['E'],
                         $tt_user['PT']."%",
                         $tt_user['TZ']);
                }
              }
              print "</TABLE>";
              print "</td></tr></table>";
            }
            print "<BR>";
            
            
            $sql_cards = "select tt_games_cards.*, tt_cards.name card_name, tt_cards.type, tt_games_herds.nik nik1, tt_games_herds1.nik nik2 
                          from tt_games_cards, tt_cards, tt_games_herds, tt_games_herds tt_games_herds1 
                          where tt_games_cards.game = ".$gameid." and
                                tt_games_cards.sts = 1 and
                                tt_cards.id = tt_games_cards.card and
                                tt_games_herds.game = tt_games_cards.game and
                                tt_games_herds.id = tt_games_cards.herd and
                                tt_games_herds1.game = tt_games_cards.game and
                                tt_games_herds1.id = tt_games_cards.on_herd
                                order by tt_games_herds1.id
                          ";
            $tt_cards = mysql_query($sql_cards);
            print mysql_error(); 
            if(mysql_num_rows($tt_cards)>0)
            {
              $td = "<TR bgcolor='#003366' align='right'><TD>%s</TD><TD>%s</TD><TD>%s</TD><TD>%s</TD><TD>%s</TD><TD>%s</TD></TR>";
              print "<p class='header'>Карты в игре</p>";

              print "<table border='0' cellspacing='0' cellpadding='0' bgcolor='#BEDEFF'><tr><td>";
              print "<TABLE border='0' width='100%' cellpadding='2' cellspacing='1'>";
              printf($td,'Id','На ком','Хозяин','Название','N','Описание');
              $i = 0;
              while($tt_card = mysql_fetch_array($tt_cards))
              {
                $sql_acts = "select tt_cards_actions.param, tt_actions.action
                             from tt_cards_actions, tt_actions
                             where tt_cards_actions.card = ".$tt_card['card']." and
                                   tt_actions.id = tt_cards_actions.action";
                $tt_acts = mysql_query($sql_acts);
                $res_act="";
                while($tt_act = mysql_fetch_array($tt_acts))
                {
                  if($tt_act['param']<>"")
                  {
                    $res_act= $res_act." ".$tt_act['action']."=".$tt_act['param'];
                  }
                  else
                  {
                    $res_act= $res_act." ".$tt_act['action'];
                  }
                }
                $res_act=$res_act." ".get_type_name($tt_card['type'] - 1);
                print mysql_error(); 
                printf($td,$tt_card['id'],$tt_card['nik2'],$tt_card['nik1'],$tt_card['card_name'],$tt_card['card'],$res_act);
              }              
              print "</TABLE>";
              print "</td></tr></table>";
            }        
            print "<BR>";

            if(($_POST['to_base']==1) and ($_POST['play_card'] <> $_POST['drop_card']))
            {
                $sql_update = 
                "
                  update tt_games_cards,tt_games_herds 
                  set tt_games_cards.on_herd = 0 
                  where tt_games_cards.sts = 0 and 
                        tt_games_cards.game=".$gameid." and
                        tt_games_herds.id = tt_games_cards.herd and 
                        tt_games_herds.game = tt_games_cards.game and 
                        tt_games_herds.user = ".$_SESSION['userid']."
                ";
                $result=mysql_query($sql_update);
                $sql_update = 
                "
                  update tt_games_cards 
                  set tt_games_cards.on_herd = ".$_POST['play_on']." 
                  where tt_games_cards.id = ".$_POST['play_card']." 
                ";
                $result=mysql_query($sql_update);
                if($_POST['drop_card']<>0)
                {
                  $sql_update = 
                  "
                    update tt_games_cards 
                    set tt_games_cards.on_herd = -1 
                    where tt_games_cards.id = ".$_POST['drop_card']." 
                  ";
                  $result=mysql_query($sql_update);
                }
            }
        
            $sql_cards = "select tt_games_cards.* 
                          from tt_games_cards,tt_games_herds 
                          where tt_games_cards.game = ".$gameid." and
                                tt_games_cards.sts = 0 and
                                tt_games_herds.id = tt_games_cards.herd and 
                                tt_games_herds.game = tt_games_cards.game and 
                                tt_games_herds.user = ".$_SESSION['userid']."
                          ";
            $tt_cards = mysql_query($sql_cards);
            print mysql_error(); 
            if(mysql_num_rows($tt_cards)>0)
            {
              $td = "<TD width='160' align='left' valign='top'>%s</TD>";
              print "<p class='header'>Ваша рука</p>";
              print "<TABLE border=0 cellpadding=0>";
              $i = 0;
              $k = 1;
              while($tt_card[$k] = mysql_fetch_array($tt_cards))
              {
                if($i==0)
                {
                  print "<TR>";
                }
                printf($td,print_herd_card($tt_card[$k]['id']));
                $i++;
                if($i==4)
                {
                  print "</TR>";
                  $i = 0;
                }
                $k=$k+1;
              }              
              print "</TABLE>";
              print "<BR>";
              $sql_herds = "select tt_games_herds.* 
                          from tt_games_herds 
                          where tt_games_herds.game = ".$gameid." and
                                tt_games_herds.sts = 1
                          ";
              $tt_herds = mysql_query($sql_herds);
              print mysql_error();
              $hc = 1;
              while($tt_herd[$hc] = mysql_fetch_array($tt_herds))
              {
                if($tt_herd[$hc]['id']==$my_id)
                {
                  $my_id_num = $hc;
                }
                $hc=$hc+1;
              }
              $hc=$hc-1;

              # zabor
              $zabor = "";
              if ($hc>3)
              {
                $sql_gcards = "select tt_games_cards.*, tt_games_herds.nik
                         from tt_games_cards, tt_games_herds 
                         where tt_games_cards.sts = 1 and 
                             tt_games_cards.herd > 0 and
                             tt_games_cards.card = 153 and
                             tt_games_cards.on_herd  = ".$my_id." and
                             tt_games_cards.game = ".$gameid." and
                             tt_games_herds.id = tt_games_cards.herd";
                $tt_gcards = mysql_query($sql_gcards);
                print mysql_error();
                while($tt_gcard = mysql_fetch_array($tt_gcards))
                {
                  if ($tt_gcard["nik"]<>$my_nik)
                  {
                    $zabor = $tt_gcard["nik"];
                  }
                }
              }

             if($tt_live==1)
             {
              print "<form action=\"\" method=\"post\">";
              print "Сыграть карту ";
              print "<select size=\"1\" name=\"play_card\">";
              $j=1;
              for($j; $j<$k; $j++)
              {
                $temp=print_name_card($tt_card[$j]['card']);
                print "<option value=".$tt_card[$j]['id'];
                if($tt_card[$j]['on_herd'] > 0)
                {
                  print " selected";
                  $t_on_herd = $tt_card[$j]['on_herd'];
                }
                print ">".$temp."(".$tt_card[$j]['id'].")</option>";
              }           
              print "</select>";
              print " на ";              
              print "<select size=\"1\" name=\"play_on\">";
              $j=1;              
              for($j; $j<=$hc; $j++)
              {
               if($tt_herd[$j]['nik']<>$zabor)
               {
                 if (sosed($j,$my_id_num,$hc)<>0)
                 {
                  print "<option value=".$tt_herd[$j]['id'];
                  if($tt_herd[$j]['id'] == $t_on_herd)
                  {
                    print " selected";
                  }
                  print ">".$tt_herd[$j]['nik']."</option>";
                 }
               }
              }               
              print "</select>";              
              print "<br><br>";
              print "Скинуть карту ";
              print "<select size=\"1\" name=\"drop_card\">";
              $j=1;
              $is_drop = 0;
              for($j; $j<$k; $j++)
              {
                $temp=print_name_card($tt_card[$j]['card']);
                print "<option value=".$tt_card[$j]['id'];
                if($tt_card[$j]['on_herd'] < 0)
                {
                  print " selected";
                  $is_drop = 1;
                }
                print ">".$temp."(".$tt_card[$j]['id'].")</option>";
              }
              print "<option value=0";
              if($is_drop==0)
              {
                print " selected";
              }
              print ">Не скидывать</option>"; 
              print "</select>";
              print "<br><br>";
              print "<input type=hidden name='HOME_ID' value='".$_POST['HOME_ID']."'>";
              print "<input type=hidden name='to_base' value=1>";
              print "<input type=\"submit\" name=\"submit\" value=\"Применить\"></form>";
             }
            }

            $tt_past_year = $tt_game['year'] - 1;
            $sql_plays = "select tt_games_cards.*,tt_games_herds.nik,tt_cards.name 
                          from tt_games_cards,tt_games_herds,tt_cards 
                          where tt_games_cards.game = ".$gameid." and
                                tt_games_cards.on_year = ".$tt_past_year." and
                                tt_games_cards.on_herd = -1 and
                                tt_games_herds.id = tt_games_cards.herd and 
                                tt_games_herds.game = tt_games_cards.game and
                                tt_cards.id = tt_games_cards.card
                          ";
            $tt_plays = mysql_query($sql_plays);
            print mysql_error();
            while($tt_play = mysql_fetch_array($tt_plays))
            {
             print $tt_play['nik']." снёс карту '".$tt_play['name']."'(".$tt_play['card'].")<br>";
            }
/*
              $sql_plays = "select tt_games_cards.*,tt_games_herds.nik nik1,tt_games_herds1.nik nik2,tt_cards.name 
                          from tt_games_cards,tt_games_herds,tt_games_herds tt_games_herds1,tt_cards 
                          where tt_games_cards.game = ".$gameid." and
                                tt_games_cards.on_year = ".$tt_past_year." and
                                tt_games_cards.on_herd > 0 and
                                tt_games_herds.id = tt_games_cards.herd and 
                                tt_games_herds.game = tt_games_cards.game and
                                tt_games_herds1.id = tt_games_cards.on_herd and 
                                tt_games_herds1.game = tt_games_cards.game and
                                tt_cards.id = tt_games_cards.card
                          ";
              $tt_plays = mysql_query($sql_plays);
              print mysql_error();
              while($tt_play = mysql_fetch_array($tt_plays))
              {
               print $tt_play['nik1']." сыграл карту '".$tt_play['name']."'(".$tt_play['card'].") на ".$tt_play['nik2']."<br>";
              }
*/              
            


            $tt_past_year = $tt_game['year'] - 1;
            $sql_cards = "select tt_games_cards.*, tt_cards.name card_name, tt_cards.type, tt_games_herds.nik nik1, tt_games_herds1.nik nik2, tt_times.name card_time 
                          from tt_games_cards, tt_cards, tt_games_herds, tt_games_herds tt_games_herds1, tt_times 
                          where tt_games_cards.game = ".$gameid." and
                                tt_games_cards.on_year = ".$tt_past_year." and
                                tt_games_cards.on_herd > 0 and
                                tt_cards.id = tt_games_cards.card and
                                tt_games_herds.game = tt_games_cards.game and
                                tt_games_herds.id = tt_games_cards.herd and
                                tt_games_herds1.game = tt_games_cards.game and
                                tt_games_herds1.id = tt_games_cards.on_herd and
                                tt_times.id = tt_cards.time
                                order by tt_games_herds1.id
                          ";
            $tt_cards = mysql_query($sql_cards);
            print mysql_error(); 
            if(mysql_num_rows($tt_cards)>0)
            {
              $td = "<TR bgcolor='#003366' align='right'><TD>%s</TD><TD>%s</TD><TD>%s</TD><TD>%s</TD><TD>%s</TD><TD>%s</TD></TR>";
              print "<p class='header'>Сыграли карты</p>";

              print "<table border='0' cellspacing='0' cellpadding='0' bgcolor='#BEDEFF'><tr><td>";
              print "<TABLE border='0' width='100%' cellpadding='2' cellspacing='1'>";
              printf($td,'Id','На ком','Хозяин','Название','N','Описание');
              $i = 0;
              while($tt_card = mysql_fetch_array($tt_cards))
              {
                $sql_acts = "select tt_cards_actions.param, tt_actions.action
                             from tt_cards_actions, tt_actions
                             where tt_cards_actions.card = ".$tt_card['card']." and
                                   tt_actions.id = tt_cards_actions.action";
                $tt_acts = mysql_query($sql_acts);
                $res_act="";
                while($tt_act = mysql_fetch_array($tt_acts))
                {
                  if($tt_act['param']<>"")
                  {
                    $res_act= $res_act." ".$tt_act['action']."=".$tt_act['param'];
                  }
                  else
                  {
                    $res_act= $res_act." ".$tt_act['action'];
                  }
                }
                $res_act=$res_act." ".$tt_card['card_time'];
                $res_act=$res_act." ".get_type_name($tt_card['type'] - 1);
                print mysql_error(); 
                printf($td,$tt_card['id'],$tt_card['nik2'],$tt_card['nik1'],$tt_card['card_name'],$tt_card['card'],$res_act);
              }              
              print "</TABLE>";
              print "</td></tr></table>";
            }        
            print "<BR>";

            $tt_past_year = $tt_game['year'] - 1;
            $sql_cards = "select tt_games_cards.*, tt_cards.name card_name, tt_cards.type, tt_games_herds.nik, tt_times.name card_time 
                          from tt_games_cards, tt_cards, tt_games_herds, tt_times 
                          where tt_games_cards.game = ".$gameid." and
                                tt_games_cards.on_year = ".$tt_past_year." and
                                tt_games_cards.on_herd > 0 and
                                tt_games_cards.herd = 0 and
                                tt_cards.id = tt_games_cards.card and
                                tt_games_herds.game = tt_games_cards.game and
                                tt_games_herds.id = tt_games_cards.on_herd and
                                tt_times.id = tt_cards.time
                                order by tt_games_herds.id
                          ";
            $tt_cards = mysql_query($sql_cards);
            print mysql_error(); 
            if(mysql_num_rows($tt_cards)>0)
            {
              $td = "<TR bgcolor='#003366' align='right'><TD>%s</TD><TD>%s</TD><TD>%s</TD><TD>%s</TD><TD>%s</TD></TR>";
              print "<p class='header'>Стихийные карты</p>";

              print "<table border='0' cellspacing='0' cellpadding='0' bgcolor='#BEDEFF'><tr><td>";
              print "<TABLE border='0' width='100%' cellpadding='2' cellspacing='1'>";
              printf($td,'Id','На ком','Название','N','Описание');
              $i = 0;
              while($tt_card = mysql_fetch_array($tt_cards))
              {
                $sql_acts = "select tt_cards_actions.param, tt_actions.action
                             from tt_cards_actions, tt_actions
                             where tt_cards_actions.card = ".$tt_card['card']." and
                                   tt_actions.id = tt_cards_actions.action";
                $tt_acts = mysql_query($sql_acts);
                $res_act="";
                while($tt_act = mysql_fetch_array($tt_acts))
                {
                  if($tt_act['param']<>"")
                  {
                    $res_act= $res_act." ".$tt_act['action']."=".$tt_act['param'];
                  }
                  else
                  {
                    $res_act= $res_act." ".$tt_act['action'];
                  }
                }
                $res_act=$res_act." ".$tt_card['card_time'];
                $res_act=$res_act." ".get_type_name($tt_card['type'] - 1);
                print mysql_error(); 
                printf($td,$tt_card['id'],$tt_card['nik'],$tt_card['card_name'],$tt_card['card'],$res_act);
              }              
              print "</TABLE>";
              print "</td></tr></table>";
            }        
            print "<BR>";


            $sql_kills = "select tt_games_cards.*,tt_cards.name 
                          from tt_games_cards,tt_cards 
                          where tt_games_cards.game = ".$gameid." and
                                tt_games_cards.kill_year = ".$tt_game['year']." and
                                tt_cards.id = tt_games_cards.card
                          ";
            $tt_kills = mysql_query($sql_kills);
            print mysql_error();
            while($tt_kill = mysql_fetch_array($tt_kills))
            {
               print " Сгорела карта '".$tt_kill['name']."'(".$tt_kill['card'].")<br>";
            }
            print "<br><br>";

            $sql_kills = "select tt_games_herds.* 
                          from tt_games_herds 
                          where tt_games_herds.game = ".$gameid." and
                                tt_games_herds.kill_year = ".$tt_game['year']." and
                                tt_games_herds.sts = 9
                          ";
            $tt_kills = mysql_query($sql_kills);
            print mysql_error();
            while($tt_kill = mysql_fetch_array($tt_kills))
            {
               print " Погибло стадо ".$tt_kill['nik']." и его трын-траву растащили.<br>";
            }
            print "<br>";


            $sql_users = "select * from tt_games_herds 
                          where tt_games_herds.user = ".$_SESSION['userid']." and 
                                tt_games_herds.game = ".$gameid;
            $tt_users = mysql_query($sql_users);
            print mysql_error(); 
            if(mysql_num_rows($tt_users)>0)
            {
              $tt_user = mysql_fetch_array($tt_users);
              if($tt_user['spy_year']==$tt_game['year'] and $tt_user['spy_year']>0)
              {
               
                $sql_cards = "select tt_games_cards.* 
                              from tt_games_cards,tt_games_herds 
                              where tt_games_cards.game = ".$gameid." and
                                   tt_games_cards.sts = 0 and
                                   tt_games_herds.id = tt_games_cards.herd and 
                                   tt_games_herds.game = tt_games_cards.game and 
                                   tt_games_herds.id = ".$tt_user['spy_herd']."
                          ";
                $tt_cards = mysql_query($sql_cards);
                print mysql_error(); 
                if(mysql_num_rows($tt_cards)>0)
                { 
                  $sql_vrags = "select tt_games_herds.*
                          from tt_games_herds
                          where tt_games_herds.id = ".$tt_user['spy_herd']." and 
                                tt_games_herds.game = ".$gameid;
                  $tt_vrags = mysql_query($sql_vrags);
                  print mysql_error(); 
                  if(mysql_num_rows($tt_vrags)>0)
                  {
                    $tt_vrag= mysql_fetch_array($tt_vrags);  
                  }
                  $td = "<TD width='160' align='left' valign='top'>%s</TD>";
                  print "<p class='header'>Отважный заяц-шпион добыл информацию о руке ".$tt_vrag['nik']."</p>";
                  print "<TABLE border=0 cellpadding=0>";
                  $i = 0;
                  $k = 1;
                 while($tt_card[$k] = mysql_fetch_array($tt_cards))
                 {
                   if($i==0)
                   {
                     print "<TR>";
                   }
                   printf($td,print_herd_card($tt_card[$k]['id']));
                   $i++;
                   if($i==4)
                   {
                     print "</TR>";
                     $i = 0;
                   }
                   $k=$k+1;
                 }              
                 print "</TABLE>";
                 print "<BR>";
                }
              } 
            }

            # pechatajem istoriju goda
            $sql_users = "select tt_games_his.*,tt_games_herds.nik 
                                 from tt_games_his,tt_games_herds 
                                 where
                                 tt_games_his.game = ".$gameid." and
                                 tt_games_his.year = ".$tt_game['year']." and
                                 tt_games_herds.id = tt_games_his.herd and
                                 tt_games_herds.game = tt_games_his.game";
            $tt_users = mysql_query($sql_users);
            print mysql_error(); 
            if(mysql_num_rows($tt_users)>0)
            {
              $td = "<TR bgcolor='#003366'><TD>%s</TD><TD>%s</TD><TD>%s</TD><TD>%s</TD><TD>%s</TD><TD>%s</TD><TD>%s</TD><TD>%s</TD><TD>%s</TD><TD>%s</TD><TD>%s</TD><TD>%s</TD></TR>";
              print "<p class='header'>История по месяцам</p>";

              print "<table border='0' cellspacing='0' cellpadding='0' bgcolor='#BEDEFF'><tr><td>";
              print "<TABLE border='0' width='100%' cellpadding='2' cellspacing='1'>";
                  printf($td,
                         "Id",
                         "Стадо",
                         "Месяц",
                         "Z",
                         "V",
                         "TT",
                         "PZ",
                         "PV",
                         "B",
                         "E",
                         "PT",
                         "TZ");
              while($tt_user = mysql_fetch_array($tt_users))
              {
                  printf($td,
                         $tt_user['herd'],
                         $tt_user['nik'],
                         $tt_user['month'],
                         $tt_user['Z'],
                         $tt_user['V'],
                         $tt_user['TT'],
                         $tt_user['PZ']."%",
                         $tt_user['PV']."%",
                         $tt_user['B'],
                         $tt_user['E'],
                         $tt_user['PT']."%",
                         $tt_user['TZ']);
              }
              print "</TABLE>";
              print "</td></tr></table>";
            }
            print "<BR>";

          }
        }  
      }
    }
?>